package com.whf.android.jar.net.utils;


import android.graphics.Bitmap;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.base.delegate.IPageLoadListener;
import com.whf.android.jar.net.RestClient;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.ISuccess;
import com.whf.android.jar.util.log.LatteLogger;

import java.util.List;
import java.util.WeakHashMap;

/**
 * 请求接口工具
 *
 * @author qf
 * @version 2022/4/2
 */
public final class RestClientUtils implements IListener {

    private final IPageLoadListener mPageLoadListener;
    private final IError mError;

    @NonNull
    public static RestClientUtils create() {
        return new RestClientUtils(null, null);
    }

    @NonNull
    public static RestClientUtils create(IError error) {
        return new RestClientUtils(null, error);
    }

    @NonNull
    public static RestClientUtils create(IPageLoadListener listener, IError error) {
        return new RestClientUtils(listener, error);
    }

    public RestClientUtils(IPageLoadListener listener, IError error) {
        this.mPageLoadListener = listener;
        this.mError = error;
    }

    @Override
    public final void inGet(String url, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .get();
    }

    @Override
    public final void inGet(@NonNull String url, String ids, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (!"/".equals(url.substring(url.length() - 1))) {
            url += "/";
        }
        url += ids;
        LatteLogger.i("url=$url");
        RestClient.builder()
                .url(url)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .get();
    }

    @Override
    public final void inGet(String url, WeakHashMap<String, Object> params, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .get();
    }

    @Override
    public void inPart(String url, WeakHashMap<String, Object> params, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .part();
    }

    @Override
    public void inPost(String url, WeakHashMap<String, Object> params,
                       WeakHashMap<String, String> files, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        LatteLogger.json("文件", GsonUtils.toJson(files));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .params(params)
                .fileSign(files)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .uploadMapFile();
    }

    @Override
    public void inBitmap(String url, Bitmap params, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .bitmap(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .bitmap();
    }


    @Override
    public final void inPost(String url, Object params, ISuccess success) {
        if (params == null) {
            ToastUtils.showShort("请检查请求参数是否正常");
            return;
        }
        if (params instanceof String) {
            inPost(url, (String) params, success);
            return;
        }
        inPost(url, GsonUtils.toJson(params), success);
    }

    @Override
    public final void inPost(String url, String data, ISuccess success) {
        if (TextUtils.isEmpty(data)) {
            data = "{}";
        }
        LatteLogger.json(data);
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .raw(data)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .post();
    }

    @Override
    public final void inPost(String url, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .post();
    }

    @Override
    public void inQuery(String url, WeakHashMap<String, Object> params, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .query();
    }

    @Override
    public final void inPut(String url, String data, ISuccess success) {
        if (TextUtils.isEmpty(data)) {
            data = "{}";
        }
        LatteLogger.json(data);
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .raw(data)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .put();
    }

    @Override
    public final void inPath(String url, @NonNull List<String> result, ISuccess success) {
        LatteLogger.i(result.toString());
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .fileArray(result)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .uploads();
    }

    @Override
    public final void inPath(String url, String data, ISuccess success) {
        LatteLogger.i(data);
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .file(data)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .upload();
    }

    @Override
    public final void download(String url, String name, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .name(name)
                .failure(this)
                .success(success)
                .error(this)
                .build()
                .download();
    }

    @Override
    public final void onFailure() {
        if (mPageLoadListener != null) {
            mPageLoadListener.hideLoading();
        } else {
            ToastUtils.showShort("连接服务器失败，请稍后再试。");
        }
    }

    @Override
    public final void onError(int code, String msg) {
        if (mError != null) {
            mError.onError(code, msg);
        } else {
            if (mPageLoadListener != null) {
                mPageLoadListener.hideLoading();
            }
        }
    }
}
